﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Xml;
using System.Text.RegularExpressions;
using CMSModel;

/// <summary>
///   新股新闻
/// </summary>
public class NewStockNewsCatch
{
    public void Import()
    {
        string reg = "<li>·<a target=\"_blank\" href=\"http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?\">[\\s\\S]{2,50}</a>[\\S\\s]<span class=\"pub_time\">[\\s\\S]{1,20}</span>[\\s\\S]{0,10}</li>";
        string newsReg = "<div id=\"Cnt-Main-Article-QQ\" bossZone=\"content\">[\\S\\s]*<!--来源、关键字微博-->";
        string endReg = "</div>[\\S\\s]{1,3}</div>[\\S\\s]{1,3}<div class=\"ft\">[\\S\\s]{1,3}<!--来源、关键字微博-->";
        string souorceReg = "<span class=\"where\">[\\S\\s]{1,100}</span>";
        for (int j = 1; j <= 2; j++)
        {
            WebClient client = new WebClient();
            string str = client.DownloadString("http://finance.qq.com/l/stock/xingu/xgdt/index.htm");
            MatchCollection mc = Regex.Matches(str, reg);
            for (int i = 0; i < mc.Count; i++)
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    string tt = mc[i].Value;
                    doc.LoadXml(tt);
                    var root = doc.DocumentElement;
                    var title = root.ChildNodes[1].InnerText;


                    var time = DateTime.Now;
                    var url = root.ChildNodes[1].Attributes["href"].Value;
                    if (IsCheck(url))
                    {
                        continue;
                    }
                    client = new WebClient();
                    string str2 = client.DownloadString(url);
                    var titleMatch = Regex.Match(str2, souorceReg);
                    string source = "";
                    if (titleMatch.Success)
                    {
                        source = Regex.Replace(titleMatch.Value, @"<[^>]*>", "");
                    }
                    str2 = Regex.Match(str2, newsReg).Value;
                    str2 = str2.Replace("<div id=\"Cnt-Main-Article-QQ\" bossZone=\"content\">", "");
                    str2 = Regex.Replace(str2, endReg, "");
                    str2 = str2.Replace("<div class=\"blkComment otherContent_01\" style=\"margin-right:0px;margin-left:0;padding-right:0px;\">", "");
                    str2 = Regex.Replace(str2, @"<a[^>]*>", "");
                    str2 = Regex.Replace(str2, "</a>", "");
                    str2 = Regex.Replace(str2, @"<img[^>]*>", "");
                    str2 = Regex.Replace(str2, "<span[^>]*>", "");
                    str2 = Regex.Replace(str2, "</span>", "");
                    str2 = NewsImportBLL.ReplaceGP(str2);
                    News news = new News();
                    news.Source = source;
                    news.step = 0;
                    news.content = str2;
                    news.ctime = DateTime.Now;
                    news.title = title;
                    news.keyword = title;
                    CMSEntities cms = new CMSEntities();
                    News_Type newsType = new News_Type();
                    newsType.NewsTypeId = "0907";
                    newsType.ctime = DateTime.Now;
                    news.News_Type.Add(newsType);
                    cms.News.AddObject(news);
                    cms.SaveChanges();
                    NewsCatch nCatch = new NewsCatch();
                    nCatch.url = url;
                    nCatch.NewsID = news.id;
                    nCatch.CTime = DateTime.Now;
                    cms.NewsCatch.AddObject(nCatch);
                    cms.SaveChanges();
                    new NewsImportBLL().ImportBk(news);
                }
                catch
                {
                }
            }
        }
    }

    bool IsCheck(string url)
    {
        CMSEntities cms = new CMSEntities();
        return cms.NewsCatch.Any(x => x.url == url);
    }

    string March(Match m)
    {
        string str = m.Value;
        return null;
    }
}